(function(mui, window, document, undefined) {
    mui.init();
    feedback.files = [];
    feedback.uploader = null;  
    var index = 1;
    var get = function (id) {

		return document.getElementById(id);
	};
	//var qsa = function(sel) {
	//	return [].slice.call(document.querySelectorAll(sel));
	//};
	var ui = {
		question: get('question'),
		contact: get('contact'),
		imageList: get('image-list'),
        submit: get('submit'),
        Address: get('Address')
	};
	ui.clearForm = function() {
		ui.question.value = '';
		ui.contact.value = '';
        ui.imageList.innerHTML = '';
        ui.Address.value = '';
        ui.newPlaceholder();
        feedback.files = [];
	};
	ui.getFileInputArray = function() {
		return [].slice.call(ui.imageList.querySelectorAll('input[type="file"]'));
	};
	ui.getFileInputIdArray = function() {
		var fileInputArray = ui.getFileInputArray();
		var idArray = [];
		fileInputArray.forEach(function(fileInput) {
			if (fileInput.value != '') {
				idArray.push(fileInput.getAttribute('id'));
			}
		});
		return idArray;
	};
	var imageIndexIdNum = 0;
	ui.newPlaceholder = function() {
		var fileInputArray = ui.getFileInputArray();
		if (fileInputArray &&
			fileInputArray.length > 0 &&
			fileInputArray[fileInputArray.length - 1].parentNode.classList.contains('space')) {
			return;
		}
		imageIndexIdNum++;
		var placeholder = document.createElement('div');
		placeholder.setAttribute('class', 'image-item space');
		var closeButton = document.createElement('div');
		closeButton.setAttribute('class', 'image-close');
		closeButton.innerHTML = 'X';
        closeButton.addEventListener('click', function (event) {
			event.stopPropagation();
            event.cancelBubble = true;
            setTimeout(function () {
                for (var temp = 0; temp < feedback.files.length; temp++) {
                    if (feedback.files[temp].name == fileInput.id) {
                        feedback.files.splice(temp, 1);
                    }
                }
				ui.imageList.removeChild(placeholder);
			}, 0);
			return false;
        }, false);
        var up = document.createElement("div");
        up.setAttribute('class', 'image-up')
		var fileInput = document.createElement('input');
        fileInput.setAttribute('type', 'file');
		fileInput.setAttribute('accept', 'image/*');
		fileInput.setAttribute('id', 'image-' + imageIndexIdNum);
        fileInput.addEventListener('change', function (event) {
          
            var event = event;
            var file = fileInput.files[0];
            //这种获取方式支持IE10  
			if (file) {
				var reader = new FileReader();
                reader.onload = function () {
					//处理 android 4.1 兼容问题
                    var base64 = reader.result.split(',')[1];
                    var dataUrl = 'data:image/png;base64,' + base64;
                    feedback.addFile(base64);
					placeholder.style.backgroundImage = 'url(' + dataUrl + ')';
                }
                var aa= reader.readAsDataURL(file);
                placeholder.classList.remove('space');
                placeholder.removeChild(up);
				ui.newPlaceholder();
			}
		}, false);
        placeholder.appendChild(closeButton);
        placeholder.appendChild(up);
		placeholder.appendChild(fileInput);
		ui.imageList.appendChild(placeholder);
	};
    ui.newPlaceholder();
    feedback.addFile = function (path) {
        feedback.files.push({ name: "image-" + index, path: path, id: "img-" + index });
        index++;
    };

    /*点击发送按钮提交问题反馈*/
	ui.submit.addEventListener('tap', function(event) {
		if (ui.question.value == '' ||
			(ui.contact.value != '' &&
				ui.contact.value.search(/^(\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+)|([1-9]\d{4,9})$/) != 0)) {
			return mui.toast('信息填写不符合规范');
		} 
		feedback.send({
			question: ui.question.value,
			contact: ui.contact.value,
            images: ui.getFileInputIdArray(),
            Address: ui.Address.value
		}, function() {
			plus.nativeUI.closeWaiting();
			mui.toast('感谢您的建议~');
			ui.clearForm();
			mui.back();
		});
    }, false);

    /**
       * 发送反馈信息
       **/
    feedback.send = function (content, callback) {
        //如果没有截图
        if (!content.images || content.images.length < 1) {
            if (callback) callback();
            return;
        }
        //如果有截图
        var ImagesPath = "";
        content.images.forEach(function (fileInputId) {
            for (var i = 0; i < feedback.files.length; i++) {
                if (feedback.files[i].name == fileInputId) {
                    ImagesPath += ',' + feedback.files[i].path + '';
                }
            }
        });
        /*整合数据*/
        Sig = 'HNHL.WebItem.Business.MessageManagement,Message';
        DateArrty = {
            ProblemDescription: content.question,
            ContactInformation: content.contact,
            SiteIncident: content.Address,
            PictureFlow: ImagesPath.substring(1),
            Sender:"冯宇"
        };
        OnAjaxSubmit(function (data) {
            mui.alert("感谢反馈，点击确定关闭", "问题反馈", "确定", function () {
                ui.clearForm();
                mui.back();
            });
        });
    };
})(mui, window, document, undefined);